package com.basketball.gym.pojo;

import com.basketball.gym.pojo.state.RoleId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author 赵肖云
 * @since 2021-03-15
 */

@Data
@ApiModel(description = "角色")
public class Role implements Serializable {
    @ApiModelProperty("角色ID")
    private Integer id;
    @ApiModelProperty("角色名")
    private String role;

    public Role() {
    }

    public Role(int id, String role) {
        this.id = id;
        this.role = role;
    }

    public Role(String role) {
        this.role = role;
        for (RoleId roleId : RoleId.values()) {
            if (roleId.getRole().equals(role)) {
                this.id = roleId.getValue();
                return;
            }
        }
    }

    public static String getRoleNameById(int id) {
        for (RoleId roleId : RoleId.values()) {
            if (roleId.getValue() == id) {
                return roleId.getRole();
            }
        }
        return "anonymity";
    }

    /**
     * 获取用户角色名
     *
     * @return roleNames
     */
    @JsonIgnore
    public static String[] getRolesName(List<Role> roles) {
        if (roles == null) {
            String[] roleName = new String[]{"anonymity"};
        }
        String[] roleNames = new String[roles.size()];
        for (int i = 0; i < roles.size(); i++) {
            roleNames[i] = roles.get(i).getRole();
        }
        return roleNames;
    }

    public static List<String> getRoles(List<Role> roleBeans) {
        List<String> roles = new ArrayList<>();
        for (Role roleBean : roleBeans) {
            roles.add(roleBean.getRole());
        }
        return roles;
    }

    /**
     * 判断roleId是否合法
     *
     * @param id：要判断的roleId
     */
    public static Boolean isTureRoleId(int id) {
        for (RoleId roleId : RoleId.values()) {
            if (roleId.getValue() == id) {
                return true;
            }
        }
        return false;
    }

    @JsonIgnore
    public static boolean isMerchant(List<Role> roleBeans) {
        for (Role roleBean :
                roleBeans) {
            if (roleBean.getId() == RoleId.MERCHANT.getValue()) {
                return true;
            }
        }
        return false;
    }
}
